﻿// 
// Copyright SmartFormat Project maintainers and contributors.
// Licensed under the MIT license.

namespace SmartFormat.Core.Parsing;

/// <summary>
/// Contains the state of the <see cref="Parser"/>.
/// Used to pass the state around to <see cref="Parser"/> methods and to reset the state.
/// </summary>
internal sealed partial class ParserState
{
    /// <summary>
    /// Creates a new instance of the <see cref="ParserState"/> class.
    /// </summary>
    public ParserState()
    {
        Index = new IndexContainer();
        InputFormat = string.Empty;
        ResultFormat = null!;
    }

    /// <summary>
    /// Initializes the state with the input format and the result format.
    /// </summary>
    public ParserState Initialize(string inputFormat, Format resultFormat)
    {
        Index.Reset();
        Index.ObjectLength = inputFormat.Length;
        InputFormat = inputFormat;
        ResultFormat = resultFormat;
        return this;
    }

    /// <summary>
    /// Clears and resets the state.
    /// </summary>
    public void Clear()
    {
        Index.Reset();
        InputFormat = string.Empty;
        ResultFormat = null!;
    }

    /// <summary>
    /// The Container for indexes pointing to positions within the input format.
    /// </summary>
    public IndexContainer Index;

    /// <summary>
    /// The input format string.
    /// </summary>
    public string InputFormat;

    /// <summary>
    /// The result format generated by the <see cref="Parser"/>.
    /// </summary>
    public Format ResultFormat;
}

